package com.tang.campus_market.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import com.tang.campus_market.dao.CocDao;
import com.tang.campus_market.dao.CooDao;
import com.tang.campus_market.dao.GoodsDao;
import com.tang.campus_market.entity.Coc;
import com.tang.campus_market.entity.Coo;
import com.tang.campus_market.entity.Goods;
import com.tang.campus_market.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Component
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;


    @Autowired
    private CooDao cooDao;

    @Autowired
    private CocDao cocDao;


    @Override
    public List<Goods> findAll(String searchText, String type) {
        EntityWrapper<Goods> wrapper = new EntityWrapper<>();
        wrapper.like("title", searchText)
                .and().like("second_class", type)
                .eq("state",1)
                .eq("sale_state",0);
        List<Goods> goods = goodsDao.selectList(wrapper);
        return goods;
    }

    public Page<Goods> queryAll(Integer page, Integer rows, String searchText, String type) {
        EntityWrapper<Goods> wrapper = new EntityWrapper<>();
        wrapper.like("title", searchText)
                .like("second_class", type)
                .eq("state",1)
                .eq("sale_state",0)
                .orderBy("id",false);
        Page<Goods> goodsPage = new Page<>(page, rows);
        List<Goods> goods = goodsDao.selectPage(goodsPage, wrapper);
        goodsPage.setRecords(goods);
        return goodsPage;
    }

    @Override
    public void deleteById(Integer id) {
        //把物品对应的评论状态更改为禁用0
        goodsDao.updateCommentStateById(id);

        //把物品对应的订单状态更改为禁用0
        goodsDao.updateOrderById(id);

        //从goods表中删除
        goodsDao.deleteById(id);
    }

    @Override
    public void updateGoodById(Integer id) {
        //把物品对应的评论状态更改为禁用0
        goodsDao.updateCommentStateById(id);

        //把物品对应的订单状态更改为禁用0
        goodsDao.updateOrderStateById(id);

        //把该物品的状态改为禁用0
        goodsDao.updateGoodById(id);
    }

    public List<Goods> queryByName(String goodsName) {
        return goodsDao.queryByName(goodsName);
    }

//    //发布商品
//    public void CreateGoods(String userId,String goodsName,String describe,Integer price,String first_class,String second_class,String image){
//        Date create_date = new Date();
//        Date modify_date = new Date();
//        goodsDao.CraeteGoods(userId,goodsName,describe,price,first_class,second_class,image,create_date,modify_date);
//    }
    //发布商品
    public void CreateGoods(Goods goods){
        goods.setCreateDate(new Date());
        goods.setModifyDate(new Date());
        goodsDao.insert(goods);
    }

    public void DeleteGood(Integer id){
        goodsDao.DeleteGood(id);
    }

    public void UpdateGood(Goods goods){
        goodsDao.updateById(goods);
    }

    //    更改商品状态
    public void updateGoodState(String goodState,Integer id){
        goodsDao.updateGoodState(goodState,id);
    }

    public void updateGoodSaleState(String goodSaleState,Integer id){
        goodsDao.updateGoodSaleState(goodSaleState,id);
    }

    @Override
    public Goods findById(Integer id) {
        Goods goods = goodsDao.selectById(id);
        return goods;
    }

    @Override
    public List<Goods> findByUserId(Integer id) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("user_id",id);
        List<Goods> goods = goodsDao.selectByMap(map);
        return goods;
    }

    @Override
    public void updateGoodOnState(Integer id) {
        goodsDao.updateGoodOnById(id);
    }


    @Override
    public boolean isController(Integer userId, Integer goodsId) {

//        Coo coo = new Coo();
//        coo.setUserId(userId);
//        coo.setGoodsId(goodsId);
//        System.out.println(coo);

        EntityWrapper<Coo> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("goods_id",goodsId);
        Integer integer = cooDao.selectCount(wrapper);

        System.out.println(integer);

        if (integer.intValue()==0){
            return false;
        }

        return true;
    }

    @Override
    public boolean controller(Integer userId, Integer goodsId) {
        EntityWrapper<Coo> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("goods_id",goodsId);
        Integer integer = cooDao.selectCount(wrapper);

        if (integer.intValue()==0){
            Coo coo = new Coo();
            coo.setUserId(userId);
            coo.setGoodsId(goodsId);
            coo.setCreateDate(new Date());
            cooDao.insert(coo);
        }

        return true;
    }

    @Override
    public boolean noController(Integer userId, Integer goodsId) {
        EntityWrapper<Coo> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("goods_id",goodsId);
        cooDao.delete(wrapper);
        return true;
    }

    @Override
    public List<Coc> findAllCollection(Integer userId) {
        EntityWrapper<Coc> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",userId);
        List<Coc> listCos = cocDao.selectList(wrapper);
        return listCos;
    }
}
